﻿Imports System.Net.Mail 'this system.net.mail allows us access to the mail classes in order for the asp.net web application to work with the mailing system.

Public Class ContactUs
    Inherits System.Web.UI.Page

    Protected Sub btnsubmitEnquiry_Click(sender As Object, e As EventArgs) Handles btnsubmitEnquiry.Click

        'This part of the coding just formats and page so when the receiver receives the email, the email address next to "Email Address will show the value entered next to the txtemail.text object


        Dim sb As New StringBuilder

        sb.Append("Email Address: ")
        sb.Append(txtemail.Text)
        sb.AppendLine()
        sb.Append("Enquiry: ")
        sb.Append(txtenquiry.Text)

        'The try statement handles any error and returns the error as a label error message which prevents the system from crashing.
        Try
            SendMail("BMW-X6Experts@hotmail.com", sb.ToString())


        Catch ex As Exception
            lblsuccesserror.Text = "Sorry there was an error, please check and try again."

        End Try



    End Sub

    Protected Sub SendMail(ByVal from As String, ByVal body As String)

        Dim email As MailMessage = New MailMessage(txtemail.Text, "BMW-X6Experts@hotmail.com", "BMW - X6 Experts Contact Us Enquiry", txtenquiry.Text)

        Dim smtpClient As SmtpClient = New SmtpClient
        smtpClient.Host = "smtp.live.com"

        smtpClient.EnableSsl = True

        Dim credentials As New System.Net.NetworkCredential("BMW-X6Experts@hotmail.com", "tailor22")

        smtpClient.Credentials = credentials

        smtpClient.Send(email)


        lblsuccesserror.Text = "Thank you for your enquiry, we have received your email and shall get back to you within 3 to 5 working days."








    End Sub

    Protected Sub btnClearForm_Click(sender As Object, e As EventArgs) Handles btnClearForm.Click
        txtemail.Text = ""
        txtenquiry.Text = ""

    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub
End Class